import { TeaCourse, CourseClass } from '@/api/course'
import { useRouter } from 'vue-router'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCourseStore = defineStore(
  'use-course-store',
  () => {
    const router = useRouter()
    const allCourse = ref([]) //全部课程
    const oneCourse = ref([]) //单个课程
    const allClass = ref([]) //全部班级

    // 全部课程
    const Id = ref('')
    const userInfo = ref('')
    // 向后传递老师的id,获取课程
    userInfo.value = JSON.parse(localStorage.getItem('userInfo'))
    Id.value = userInfo.value.teacherId
    TeaCourse(Id).then((res) => {
      // console.log(res);
      allCourse.value = res.data
    })

    //   点击的课程
    const toCourseDetail = (id) => {
      allCourse.value.forEach((item) => {
        if (id == item.courseId) {
          oneCourse.value = item
          console.log(oneCourse)
          localStorage.setItem('course', JSON.stringify(oneCourse.value))

          const CourseId = ref('')
          const getCourseId = ref('')
          // 向后传递老师的id，获取班级
          getCourseId.value = JSON.parse(localStorage.getItem('course'))
          CourseId.value = getCourseId.value.courseId
          CourseClass(CourseId).then((res) => {
            console.log(res.data)
            allClass.value = res.data
          })

          router.push(`/coursedetail/${id}`)
        }
      })
    }

    return {
      allCourse,
      toCourseDetail,
      oneCourse,
      allClass
    }
  },
  {
    persist: true
  }
)
